﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using UI.WebPage.PageBase;
using Model.AuthModel;
using BLL.AuthModel;
using Util.Configuration;
using Com.Ompchina.Lib.Text;
namespace Com.Ompchina.Web.admin.main
{
    public partial class changepass : AuthModelPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        #region 页面私有方法
        /// <summary>
        /// 检查输入的信息
        /// </summary>
        /// <returns></returns>
        private bool CheckInput()
        {
            if (string.IsNullOrEmpty(txtOldPassword.Text.Trim()))
            {
                base.SetMessage(false, "请输入当前用户的旧密码");
                return false;
            }
            else if (string.IsNullOrEmpty(txtNewPassword.Text.Trim()))
            {
                base.SetMessage(false, "请输入新密码");
                return false;
            }
            else if (string.IsNullOrEmpty(txtCfmPassword.Text.Trim()))
            {
                base.SetMessage(false, "请输入确认密码");
                return false;
            }
            else if (!txtNewPassword.Text.Trim().Equals(txtCfmPassword.Text.Trim()))
            {
                base.SetMessage(false, "新密码和确认密码不一致");
                return false;
            }
            return true;
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.CheckInput())
            {
                StaffInfo info = new StaffBLL().GetStaffByID(base.LoginStaff.StaffID);
                if (!info.Password.Equals(Security.MD5(txtOldPassword.Text.Trim())))
                {
                    base.SetMessage(false, "旧密码输入错误，不能修改");
                }
                else if (info.Password.Equals(Security.MD5(txtNewPassword.Text.Trim())))
                {
                    base.SetMessage(false, "新密码和旧密码一致，不必修改");
                }
                else
                {
                    string message = string.Empty;
                    if (new StaffBLL().UpdateStaff(info.StaffID, info.Username, Security.MD5(txtNewPassword.Text.Trim()), info.Email, info.IsLock, out message))
                    {
                        base.SetMessage(true, "密码修改成功，请重新登录");
                        base.Redirect(WebConfiguration.WebRoot + WebConfiguration.AdminRoot + "/WebHandlers/user/logout.ashx", 1);
                    }
                    else
                        base.SetMessage(false, "密码修改错误，请稍后重试");
                }
            }
        }
        #endregion
    }
}
